<template>
  <el-card>
    <!-- 标题 + 新增按钮 -->
    <template #header>
      <div class="flex-between">
        <span>诊断目录管理</span>
        <el-button type="primary" @click="openDialog()">新增诊断</el-button>
      </div>
    </template>

    <!-- 查询区 -->
    <el-form :inline="true" class="mb-3">
      <el-form-item label="关键字">
        <el-input
            v-model="query.keyword"
            placeholder="诊断编码 / 名称"
            clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadData()">查询</el-button>
        <el-button @click="reset()">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 列表（已删除 ID 列） -->
    <el-table :data="tableData" stripe border style="width:100%">
      <el-table-column prop="dicacode"   label="诊断编码" width="120" />
      <el-table-column prop="dicaname"   label="诊断名称" />
      <el-table-column prop="sequenceno" label="顺序号" width="100" />
      <el-table-column prop="dicatype"   label="诊断类型" width="100" />
      <el-table-column label="操作" width="180">
        <template #default="{ row }">
          <el-button size="small" type="primary" @click="openDialog(row)">
            编辑
          </el-button>
          <el-button size="small" type="danger" @click="remove(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="mt-3 text-right">
      <el-pagination
          :current-page="query.page"
          :page-size="query.pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          :page-sizes="[5,10,20,50]"
          @current-change="p => { query.page = p; loadData() }"
          @size-change="s => { query.pageSize = s; loadData() }"
      />
    </div>
  </el-card>

  <!-- 新增 / 编辑对话框 -->
  <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="500px"
      @close="onClose"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
      <el-form-item prop="dicacode" label="诊断编码">
        <el-input v-model="form.dicacode" />
      </el-form-item>
      <el-form-item prop="dicaname" label="诊断名称">
        <el-input v-model="form.dicaname" />
      </el-form-item>
      <el-form-item prop="sequenceno" label="顺序号">
        <el-input-number v-model="form.sequenceno" :min="0" style="width:100%" />
      </el-form-item>
      <el-form-item prop="dicatype" label="诊断类型">
        <el-input-number v-model="form.dicatype" :min="0" style="width:100%" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="save()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus'

// 路由前缀
const entity = 'diag'  // 对应 /api/master/diag

// 查询条件与数据
const query = reactive({ keyword: '', page: 1, pageSize: 10 })
const tableData = ref<any[]>([])
const total = ref(0)

// 对话框相关
const dialogVisible = ref(false)
const dialogTitle = ref('新增诊断')
const form = reactive<any>({
  id: undefined,
  dicacode: '',
  dicaname: '',
  sequenceno: 1,
  dicatype: 1
})
const formRef = ref<FormInstance>()

// 校验规则
const rules = {
  dicacode:   [{ required: true, message: '请输入诊断编码', trigger: 'blur' }],
  dicaname:   [{ required: true, message: '请输入诊断名称', trigger: 'blur' }],
  sequenceno: [{ required: true, type: 'number', message: '请输入顺序号', trigger: 'change' }],
  dicatype:   [{ required: true, type: 'number', message: '请输入诊断类型', trigger: 'change' }],
}

// 拉取列表
async function loadData() {
  const { data } = await axios.get(`/api/master/${entity}`, { params: query })
  tableData.value = data.records
  total.value = data.total
}

// 重置查询
function reset() {
  query.keyword = ''
  query.page = 1
  query.pageSize = 10
  loadData()
}

// 打开对话框
function openDialog(row?: any) {
  if (row) {
    Object.assign(form, row)
    dialogTitle.value = '编辑诊断'
  } else {
    Object.assign(form, {
      id: undefined,
      dicacode: '',
      dicaname: '',
      sequenceno: 1,
      dicatype: 1
    })
    dialogTitle.value = '新增诊断'
  }
  dialogVisible.value = true
}

// 保存（新增或更新）
async function save() {
  await formRef.value?.validate()
  if (form.id) {
    await axios.put(`/api/master/${entity}/${form.id}`, form)
    ElMessage.success('更新成功')
  } else {
    await axios.post(`/api/master/${entity}`, form)
    ElMessage.success('新增成功')
  }
  dialogVisible.value = false
  loadData()
}

// 删除
async function remove(row: any) {
  try {
    await ElMessageBox.confirm('确认删除吗？', '提示', { type: 'warning' })
    await axios.delete(`/api/master/${entity}/${row.id}`)
    if (tableData.value.length === 1 && query.page > 1) {
      query.page--
    }
    ElMessage.success('删除成功')
    loadData()
  } catch {
    // 取消或失败不提示
  }
}

// 关闭时清除校验
function onClose() {
  formRef.value?.clearValidate()
}

// 组件挂载后加载数据
onMounted(loadData)
</script>

<style scoped>
.flex-between { display: flex; justify-content: space-between; align-items: center; }
.mb-3         { margin-bottom: 12px; }
.mt-3         { margin-top: 12px; }
.text-right   { text-align: right; }
</style>
